package com.sxj.shop.servlet;

import com.sxj.shop.entity.User;
import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserExistException;
import com.sxj.shop.exception.UserNotExistException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.UserService;
import com.sxj.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 * @date 2024年03月13日 20:22
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/register.user":
                register(request, response);
                break;
            case "/login.user":
                login(request, response);
                break;
            case "/logout.user":
                logout(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.print("<h1 style='color:red;'>您输入的请求有误,请检查请求是否正确</h1>");
        }
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        try {
            // 实现注册功能
            // 调用业务层方法实现对应的业务逻辑
            userService.register(username, password, phone, address);
            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (UserExistException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (Exception e) {
            request.setAttribute("registerMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        }
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            // 实现登录功能
            User user = userService.login(username, password);
            // 登录成功之后将用户信息保存到session中
            request.getSession().setAttribute("user",user);
            // 当用户成功登陆之后,向session中保存一个新的购物车,专属于当前用户
            request.getSession().setAttribute("cart",new Cart());
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            request.setAttribute("loginMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath()+"/findAll.product");
    }
}
